﻿using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using VASJ.Common;
using VASJ.Model;
using VASJ.VASJService;

namespace VASJ.Business
{
    public class SercurityService
    {
        public static AcpInetResult Login(string username, string password)
        {
            try
            {
                VASJService.AcpInetServiceClient acpiNet = new VASJService.AcpInetServiceClient();
                acpiNet.ClientCredentials.UserName.UserName = "acpinet_service";
                acpiNet.ClientCredentials.UserName.Password = "acpinet_service";
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

                AcpInetResult rtValue = acpiNet.Login(username, password);

                return rtValue;
            }
            catch
            {
                string messageBoxLoginFailed = ResourceManager.GetResourceTitle("frmLogin.LoginFailed", "vi-VN");
                throw new CustomException(messageBoxLoginFailed);
            }
        }

        public static AcpInetResult CreateUser(string username, string password)
        {
            VASJService.AcpInetServiceClient acpiNet = new VASJService.AcpInetServiceClient();
            acpiNet.ClientCredentials.UserName.UserName = "acpinet_service";
            acpiNet.ClientCredentials.UserName.Password = "acpinet_service";
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

            AcpInetResult rtValue = acpiNet.CreateUser(username, password);

            return rtValue;
        }

        public static AcpInetResult CreateNewUser(string username, string password, string email)
        {
            VASJService.AcpInetServiceClient acpiNet = new VASJService.AcpInetServiceClient();
            acpiNet.ClientCredentials.UserName.UserName = "acpinet_service";
            acpiNet.ClientCredentials.UserName.Password = "acpinet_service";
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

            AcpInetResult rtValue = acpiNet.CreateNewUser(username, password, email);

            return rtValue;
        }

        public static AcpInetResult ChangePassword(string username, string oldpassword, string newpassword)
        {
            VASJService.AcpInetServiceClient acpiNet = new VASJService.AcpInetServiceClient();
            acpiNet.ClientCredentials.UserName.UserName = "acpinet_service";
            acpiNet.ClientCredentials.UserName.Password = "acpinet_service";
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

            AcpInetResult rtValue = acpiNet.ChangePassword(username, oldpassword, newpassword);

            return rtValue;
        }

        public static AcpInetResult Logout(System.Guid token)
        {
            VASJService.AcpInetServiceClient acpiNet = new VASJService.AcpInetServiceClient();
            acpiNet.ClientCredentials.UserName.UserName = "acpinet_service";
            acpiNet.ClientCredentials.UserName.Password = "acpinet_service";
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

            AcpInetResult rtValue = acpiNet.Logout(token);

            return rtValue;
        }
    }
}
